<template>
  <el-card class="fontAuth">
    <h1>按钮权限控制</h1>
    <CurrentPermissionMode />
    <div class="role">当前拥有的code列表: {{ permCodeList }}</div>

    <el-alert
      title="点击后请查看按钮变化(必须处于后台权限模式才可测试此页面所展示的功能)"
      type="info"
      show-icon
      :closable="false"
    >
    </el-alert>

    <div style="margin-top: 20px">
      <el-button type="primary" @click="switchToken(2)" :disabled="!isBackPremissionMode"
        >点击切换按钮权限(用户id为2)</el-button
      >
      <el-button type="primary" @click="switchToken(1)" :disabled="!isBackPremissionMode"
        >点击切换按钮权限(用户id为1,默认)</el-button
      >
    </div>

    <el-divider>组件方式判断权限(有需要可以自行全局注册)</el-divider>
    <Authority :value="'1000'">
      <el-button type="primary"> 拥有code ['1000']权限可见 </el-button>
    </Authority>
    <Authority :value="'2000'">
      <el-button type="success"> 拥有code ['2000']权限可见 </el-button>
    </Authority>
    <Authority :value="['1000', '2000']">
      <el-button type="danger"> 拥有code ['1000','2000']角色权限可见 </el-button>
    </Authority>

    <el-divider>函数方式方式判断权限(适用于函数内部过滤)</el-divider>
    <el-button v-if="hasPermission('1000')" type="primary"> 拥有code ['1000']权限可见 </el-button>
    <el-button v-if="hasPermission('2000')" type="success"> 拥有code ['2000']权限可见 </el-button>
    <el-button v-if="hasPermission(['1000', '2000'])" type="danger">
      拥有code ['1000','2000']角色权限可见
    </el-button>

    <el-divider>自定义指令方式方式判断权限(该方式不能动态修改权限，不是响应式的)</el-divider>
    <el-button v-auth="'1000'" type="primary"> 拥有code ['1000']权限可见 </el-button>
    <el-button v-auth="'2000'" type="success"> 拥有code ['2000']权限可见 </el-button>
    <el-button v-auth="['1000', '2000']" type="danger">
      拥有code ['1000','2000']角色权限可见
    </el-button>
  </el-card>
</template>

<script lang="ts">
import { defineComponent, computed } from 'vue'
import CurrentPermissionMode from '../CurrentPermissionMode.vue'
// 使用该方法导入useStore可以加入ts提示
import { useStore } from '@/store'
import { RoleEnum } from '@/enums/roleEnum'
import { usePermission } from '@/hooks/web/usePermission'
import { Authority } from '@/components/Authority/index'
import projectSetting from '@/settings/projectSetting'
import { PermissionModeEnum } from '@/settings/types'

export default defineComponent({
  components: {
    CurrentPermissionMode,
    Authority
  },
  setup() {
    const store = useStore()
    const { hasPermission } = usePermission()

    const permCodeList = computed(() => store.state.permCodeList)

    async function switchToken(userId: number) {
      // 本函数切换用户登录Token的部分仅用于演示，实际生产时切换身份应当重新登录
      if (userId === 1) {
        store.commit('setPermCodeList', ['1000', '3000', '5000'])
      } else if (userId === 2) {
        store.commit('setPermCodeList', ['2000', '4000', '6000'])
      }
    }

    return {
      permCodeList,
      isSuper: computed(() => store.state.login.roleList.includes(RoleEnum.SUPER)),
      isTest: computed(() => store.state.login.roleList.includes(RoleEnum.TEST)),
      switchToken,
      RoleEnum,
      hasPermission,
      isBackPremissionMode: projectSetting.permissionMode === PermissionModeEnum.BACK
    }
  }
})
</script>

<style lang="scss" scoped>
.fontAuth {
  font-size: 16px;

  h1 {
    font-size: 24px;
    padding: 10px 0;
  }

  .role {
    padding: 10px 0;
  }
}
</style>
